package com.android.kotlincoroutinemvvmdemo.view
import android.content.Context
import android.graphics.Bitmap
import android.graphics.Camera
import android.graphics.Canvas
import android.graphics.Matrix
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import androidx.core.graphics.applyCanvas
import com.android.kotlincoroutinemvvmdemo.R

/*
* LockUnlockView
* */
class LockUnlockView @JvmOverloads constructor(
    context: Context,
    attributeSet: AttributeSet? = null
) : BaseView(context, attributeSet) {

    private var mSpace0 = 0F
    private var mSpace1 = 0F
    private var mSpace2 = 0F

    private val lineWidth = context.resources.getDimension(R.dimen.dp_2)

//    private var mBitmap: Bitmap? = null


    private val mPaint0 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            strokeJoin = Paint.Join.ROUND
            strokeWidth = lineWidth
            color = context.resources.getColor(R.color.color_FF9100)
            style = Paint.Style.STROKE
        }
    }

    private val mPaint1 by lazy {
        Paint().apply {
            isAntiAlias = true
            isDither = true
            strokeCap = Paint.Cap.ROUND
            color = context.resources.getColor(R.color.color_FF9100)
            style = Paint.Style.FILL
        }
    }

    private val mPath0 by lazy {
        Path()
    }

//    private val mMatrix by lazy {
//        Matrix()
//    }

//    private val mCamera by lazy {
//        Camera()
//    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        setBackgroundColor(context.resources.getColor(R.color.color_F3F3F3))
        canvas?.apply {
            val cx = mWidth / 2.0F
            val cy = mHeight / 2.0F
            mSpace0 = cx / 2
            mSpace1 = cx / 3 - lineWidth / 6
            mSpace2 = cx / 10
            val offsetY = (mSpace0 - Math.abs(mSpace1 - mSpace0)) / 2
            translate(cx, cy)
            mPath0.apply {
                moveTo(
                    -cx + mSpace1,
                    -cy + mSpace0 + offsetY
                )
                lineTo(
                    cx - mSpace1,
                    -cy + mSpace0 + offsetY
                )
                lineTo(
                    cx - mSpace1,
                    cy - mSpace0 + offsetY
                )
                lineTo(
                    -cx + mSpace1,
                    cy - mSpace0  + offsetY
                )
                close()
            }
            drawPath(mPath0, mPaint0)

            save()
            rotate(90F, 0F, offsetY)
            drawRoundRect(
                -mSpace1 * 3 / 4,
                -mSpace2 / 2  + offsetY,
                mSpace1 * 3 / 4,
                mSpace2 / 2  + offsetY,
                10F,
                10F,
                mPaint1
            )
            restore()

            drawArc(
                    -mSpace1 /*- mSpace1 * 2*/,
                -cy + mSpace0  + offsetY - mSpace1,
                    mSpace1 /*- mSpace1 * 2*/,
                -cy + mSpace0  + offsetY + mSpace1,
                    180F,
                    180F,
                    false,
                    mPaint0
                )

//            if (mBitmap == null) {
//                mBitmap = Bitmap.createBitmap(
//                    (mSpace0 * 4).toInt(),
//                    (mSpace0 * 4).toInt(),
//                    Bitmap.Config.ARGB_8888
//                )
//            }
//            val createBmp = mBitmap!!.applyCanvas {
//                drawArc(
//                    mSpace0,
//                     mSpace0 / 2,
//                    mSpace0 * 3,
//                    mSpace0 * 2 + mSpace0 / 2,
//                    180F,
//                    180F,
//                    false,
//                    mPaint0
//                )
//            }
//
//            mMatrix.reset()
//            mCamera.save()
//            //mCamera.rotateY(0F)
//            //mCamera.rotateZ(40F)
//            //mCamera.setLocation(mSpace0, mSpace0 / 2,0F)
//            mCamera.getMatrix(mMatrix)
//            mCamera.restore()
//
//            //mMatrix.preTranslate(-mSpace0, -mSpace0 * 1 + mSpace0 / 2)
//            //mMatrix.postTranslate(mSpace0, mSpace0 * 1 + mSpace0 / 2)
//            //mMatrix.preRotate(-5F,-0F, -cy)
//            //mMatrix.postRotate(5F, 0F, cy)
//            canvas.concat(mMatrix)
//
//            //mMatrix.setTranslate(0F,0F)
//            drawBitmap(createBmp, mMatrix, null)
        }
    }

    companion object {
        const val TAG = "LockUnlockView"
    }
}